<?php
/**
 *
 * @author: xaboy<365615158@qq.com>
 * @day: 2017/11/11
 */

namespace app\base\model\system;

use app\base\model\ModelTrait;
use think\Model;
use app\core\exception\BusinessException;
use app\core\exception\ErrorCode;
use app\core\util\{TokenUtil, ToolUtil};

/**
 * Class SystemAdmin
 * @package app\base\model\system
 */
class SystemAdmin extends Model
{
    use ModelTrait;

    protected $table = 'xy_system.xy_system_admin';

    const LOGIN_ERROR_SESSION_KEY = 'ADMIN_LOGIN_ERROR';

    /**
     * Desc: 用户登陆
     * Date: 2019/10/21 21:03
     * Author: yiyu
     * @param $admin_name
     * @param $pwd
     * @return bool
     */
    public static function login($admin_name, $pwd)
    {
        $adminInfo = self::get(compact('admin_name'));

        if (!$adminInfo) {
            throw new BusinessException(ErrorCode::USER_NOT_FOUND);
        }

        if ($adminInfo['admin_pwd'] != ToolUtil::getEncryptPwd($pwd, $adminInfo['pwd_salt'])) {
            throw new BusinessException(ErrorCode::USER_LOGIN_PASSWORD_ERROR);
        }

        if (!$adminInfo['status']) {
            throw new BusinessException(ErrorCode::USER_ACCOUNT_DISABLE);
        }

        return $adminInfo['admin_id'];
    }

    /**
     * Desc: 获得登陆用户信息
     * Date: 2019/11/28 0:05
     * Author: yiyu
     * @param $admin_id
     * @return array
     */
    public static function getActiveAdminInfo($admin_id)
    {
        $adminInfo = self::find(compact('admin_id'));
        if (!$adminInfo) {
            throw new BusinessException(ErrorCode::USER_NOT_LOGIN);
        }

        if ($adminInfo['status'] == 0) {
            throw new BusinessException(ErrorCode::USER_ACCOUNT_DISABLE);
        }

        if ($adminInfo['is_del'] == 1) {
            throw new BusinessException(ErrorCode::USER_ACCOUNT_DISABLE);
        }
        return $adminInfo;
    }

    /**
     * Desc: 获取管理员权限信息
     * Date: 2019/11/28 0:05
     * Author: yiyu
     * @param $adminInfo
     * @return array|null
     */
    public static function getActiveAdminAuth($adminInfo = [])
    {
        if (empty($adminInfo)) {
            $adminId = TokenUtil::getAdminIdByToken();
            $adminInfo = self::getActiveAdminInfo($adminId);
        }
        return $adminInfo['level'] === 0 ? SystemRole::getAllAuth() : SystemRole::rolesByAuth($adminInfo['role_ids']);
    }


    /**
     * @param $field
     * @param $level
     * @return false|\PDOStatement|string|\think\Collection
     */
    public static function getOrdAdmin($field = 'real_name,id', $level = 0)
    {
        return self::where('level', '>=', $level)->field($field)->select();
    }

    public static function getTopAdmin($field = 'real_name,admin_id')
    {
        return self::where('level', 0)->field($field)->select();
    }

    /**
     * Desc: 获取管理员列表
     * Date: 2019/10/21 21:06
     * Author: yiyu
     * @param $where
     * @return false|\PDOStatement|string|\think\Collection
     */
    public static function getAdminList($where)
    {
        $model = new self;
        if ($where['admin_name']) {
            $model = $model->where('admin_name', 'LIKE', '%' . (string)$where['admin_name'] . '%');
        }
        if ($where['real_name']) {
            $model = $model->where('real_name', (string)$where['real_name']);
        }
        if ($where['role_ids']) {
            $model = $model->where("CONCAT(',',role_ids,',')  LIKE '%,$where[role_ids],%'");
        }
        $items = $model->where('level', '=', bcadd($where['level'], 1, 0))->where('is_del', 0)->select();
        if ($items) {
            foreach ($items as &$item) {
                $item['rule_list'] = SystemMenus::where('menu_id', 'IN',
                    array_unique(explode(',', $item['role_ids'])))->where('level',
                    bcadd($where['level'], 1, 0))->column('menu_name');
            }
        }

        return $items;
    }
}